package com.two.lbms.syt.service;

import com.two.lbms.base.BaseService;
import com.two.lbms.dao.BookInfoMapper;
import com.two.lbms.dao.CollectionListMapper;
import com.two.lbms.dao.LendListMapper;
import com.two.lbms.dao.ReaderMapper;
import com.two.lbms.syt.dto.LendDTO;
import com.two.lbms.utils.AssertUtil;
import com.two.lbms.vo.CollectionList;
import com.two.lbms.vo.LendList;
import jakarta.annotation.Resource;

import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;

@Service
public class LendService extends BaseService<LendList,Integer> {
    @Resource
    private LendListMapper lendListMapper;
    @Resource
    private CollectionListMapper collectionListMapper;
    @Resource
    private BookInfoMapper bookInfoMapper;
    @Resource
    private ReaderMapper readerMapper;

    /**
     * 添加借书记录
     *  //根据bookId获取图书信息
     *     //根据bookId查找图书库存是否为空
     *     //根据bookId修改馆藏表图书数量-1
     *     //添加借阅记录表信息：借阅时间/更新时间/借阅时间/到期时间/图书状态+1
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void addLentList(LendDTO lendDTO) {
        AssertUtil.isTrue(StringUtils.isBlank(lendDTO.getBookName()),"图书名字不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(lendDTO.getReaderName()),"读者姓名不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(lendDTO.getBookStatus()),"图书状态不能为空");

        //参数判空
        Integer bookId = bookInfoMapper.selectByPrimaryName(lendDTO.getBookName()).getId();
        Integer readerId = readerMapper.selectByPrimaryName(lendDTO.getReaderName()).getId();
        AssertUtil.isTrue(bookId==null,"该图书不存在");
        AssertUtil.isTrue(readerId==null,"该读者不存在");
        //根据bookId查找图书库存是否为空
        CollectionList collectionList = collectionListMapper.selectByBookId(bookId);
        AssertUtil.isTrue(collectionList==null,"图书尚未入库");
        AssertUtil.isTrue(collectionList.getBookCount()==0,"该图书无库存");

        //添加默认借阅记录
        // 添加借阅记录表信息：借阅时间/更新时间/借阅时间/到期时间/图书状态+1
        LendList lendList = new LendList();
        lendList.setBookId(bookId);
        lendList.setReaderId(readerId);
        lendList.setBookStatus(lendDTO.getBookStatus());
        lendList.setLendDate(new Date());
        //设置到期时间 增加一个月
        Calendar instance = Calendar.getInstance();
        instance.setTime(lendDTO.getLendDate());
        instance.add(Calendar.MONTH,1);
        lendList.setDueDate(instance.getTime());
        lendList.setCreateTime(LocalDateTime.now());
        lendList.setUpdateTime(LocalDateTime.now());
        lendList.setIsValid(1);

        //根据bookId和readerId添加借阅记录
        AssertUtil.isTrue(lendListMapper.insertSelective(lendList)<1,"添加借阅记录失败");
        //修改馆藏表图书数量-1
        collectionList.setBookCount(collectionList.getBookCount() - 1);
        AssertUtil.isTrue(collectionListMapper.updateBookCount(collectionList)<1,"修改馆藏失败");

    }

    /**
     * 修改借书记录
     * @param
     * @param
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateLentMsg(LendDTO lendDTO) {
        //判断更新参数
        AssertUtil.isTrue(StringUtils.isBlank(lendDTO.getReaderName()),"借阅人不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(lendDTO.getBookName()),"书籍名字不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(lendDTO.getBookStatus()),"借书状态不能为空");
        AssertUtil.isTrue(lendDTO.getLendDate() == null,"借阅时间不能为空");
        //设置更新数据
        //执行更新操作
        LendList lendList = new LendList();
        Integer readerId = readerMapper.selectByPrimaryName(lendDTO.getReaderName()).getId();
        Integer bookId = bookInfoMapper.selectByPrimaryName(lendDTO.getBookName()).getId();
        lendList.setId(lendDTO.getId());
        lendList.setBookId(bookId);
        lendList.setReaderId(readerId);
        lendList.setBookStatus(lendDTO.getBookStatus());
        lendList.setUpdateTime(LocalDateTime.now());
        AssertUtil.isTrue(lendListMapper.updateByPrimaryKeySelective(lendList)<1,"更新失败");
    }

    /**
     * 删除借阅记录
     * @param ids
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteLendList(Integer[] ids){
        //判断id是否存在
        AssertUtil.isTrue(ids==null||ids.length==0,"待删除记录不存在");
        //执行删除操作
        AssertUtil.isTrue(lendListMapper.deleteBatch(ids)!=ids.length,"删除记录失败");
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public LendDTO queryById(Integer lendId) {
        LendDTO lendDTO =lendListMapper.queryById(lendId);
        return lendDTO;
    }
}
